﻿/* ----------------------------------------------------------------------------
 * Copyright (c) Philip Stears 
 *
 * This source code is subject to terms and conditions of the Microsoft Public 
 * License (Ms-PL).
 * 
 * By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 * 
 * To request a copy of the Microsoft Public License, send a request by email
 * to license@connectbasic.net.
 *
 * This notice must not be removed from this software.
 * ---------------------------------------------------------------------------- */

namespace ConnectBasic.Shell.Implementation
{
    using System;
    using System.Windows.Forms;
    using ConnectBasic.Shell.Extensibility;

    internal partial class SolutionExplorerToolWindow : UserControl
    {
        internal const string InvariantName = "SolutionExplorer";

        private IShell mShell;

        #region .ctor

        public SolutionExplorerToolWindow(IShell shell)
        {
            if (shell == null) throw new ArgumentNullException("shell");

            // Initialize designer stuff
            InitializeComponent();

            // Make sure we know when the project
            // collection changes
            mShell = shell;
            mShell.Projects.ProjectLoaded += new EventHandler<ProjectEventArgs>(Projects_ProjectLoaded);

            // Load any existing projects
            LoadProjects();
        }

        #endregion

        #region Shell Handling

        void Projects_ProjectLoaded(object sender, ProjectEventArgs e)
        {
            LoadProject(e.Project);
        }

        void LoadProjects()
        {
            SolutionTree.BeginUpdate();

            foreach (var project in mShell.Projects)
            {
                LoadProject(project);
            }

            SolutionTree.ExpandAll();
            SolutionTree.EndUpdate();
        }

        void LoadProject(IProject project)
        {
            var node = SolutionTree.Nodes.Add(project.DisplayName);
            node.Tag = project;
            node.ImageKey = node.SelectedImageKey = "Project";

            foreach (var item in project.Items)
            {
                var itemNode = node.Nodes.Add(item.Name);
                itemNode.Tag = item;

                if (SolutionImages.Images.ContainsKey(item.SmallImageKey))
                {
                    itemNode.ImageKey = itemNode.SelectedImageKey = item.SmallImageKey;
                }
                else
                {
                    var image = item.GetSmallImage();

                    if (image != null)
                    {
                        SolutionImages.Images.Add(item.SmallImageKey, image);
                        itemNode.ImageKey = itemNode.SelectedImageKey = item.SmallImageKey;
                    }
                }
            }
        }

        #endregion

        #region UI Event Handlers 
        
        private void SolutionTree_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            var editableItem = e.Node.Tag as IEditable;

            if (editableItem != null)
            {
                editableItem.TryEdit();
            }
        }

        #endregion
    }
}
